<?php
namespace app\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\Validate;

class Logout{
    public function index()
    {
        session_start();

        
    
        try {
            // 日志记录会话状态
            error_log('Before unsetting session, usernum: ' . ($_SESSION['user']['usernum'] ?? 'Not set'));
    
            // 清除会话的 `usernum`
            if (isset($_SESSION['user']['usernum'])) {
                unset($_SESSION['user']['usernum']);
            }
    
            // 日志记录会话状态
            error_log('After unsetting usernum, session: ' . print_r($_SESSION, true));
    
            // 完全清除会话
            session_unset();
            session_destroy();
    
            // 清除 Cookie
            if (isset($_COOKIE['PHPSESSID'])) {
                setcookie('PHPSESSID', '', time() - 3600, '/', '', true, true);
            }
    
            return json(['code' => 0, 'msg' => '退出登录成功']);
        } catch (\Exception $e) {
            error_log($e->getMessage());
            return json(['code' => 1, 'msg' => '退出登录时发生错误']);
        }
    }
}